package com.prestamomo.vil.ppa.pmvilpage.pmrot;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.adjust.sdk.Adjust;
import com.adjust.sdk.AdjustEvent;
import com.prestamomo.vil.ppa.BuildConfig;
import com.prestamomo.vil.ppa.R;
import com.prestamomo.vil.ppa.pmvilapi.PmvilCheckPage;
import com.prestamomo.vil.ppa.pmvilapi.PmvilHttpRequest;
import com.prestamomo.vil.ppa.pmvilmedium.pmviluse.PmvilAtyMd;
import com.prestamomo.vil.ppa.pmvilmedium.pmviluse.PmvilSpMd;
import com.prestamomo.vil.ppa.pmvilmedium.pmviluse.PmvilTosMd;
import com.prestamomo.vil.ppa.pmvilnet.PmvilAjustinfse;
import com.prestamomo.vil.ppa.pmvilnet.pmvillistener.PmvilCallBack;
import com.prestamomo.vil.ppa.pmvilpage.PmvilBlockVy;
import com.prestamomo.vil.ppa.pmvilpage.PmvilCensorVy;
import com.prestamomo.vil.ppa.pmvilpage.PmvilListingVy;
import com.prestamomo.vil.ppa.pmvilpage.PmvilLoginVy;
import com.prestamomo.vil.ppa.pmvilpage.PmvilPlaceVy;
import com.prestamomo.vil.ppa.pmvilpage.PmvilSellerVy;
import com.prestamomo.vil.ppa.pmvilpage.PmvilSourceVy;
import com.prestamomo.vil.ppa.pmvilpage.PmvilWebAtVy;
import com.prestamomo.vil.ppa.pmvilpage.pmrot.load.PmvilCustomDlg;

public abstract class PmvilAtyRoot extends AppCompatActivity {

    private PmvilCustomDlg customPmvilDialog;
    public AppCompatActivity pmvilAy;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setTheme(androidx.appcompat.R.style.Widget_AppCompat_Light_ActionBar);
        setContentView(getPmvilLayoutId());
        pmvilAy = this;
        initPmvilRootView();
    }

    private void initPmvilRootView() {
        PmvilAtyMd.pushPmvilAty(pmvilAy);
        showPmvilUi();
        clickePmvil();
        loadPmvilData();
    }

    protected abstract int getPmvilLayoutId();

    public void showPmvilLoading() {
        if (customPmvilDialog == null) {
            customPmvilDialog = new PmvilCustomDlg(this, "載入中...");
        }
        if (customPmvilDialog != null&&!customPmvilDialog.isShowing()) {
            showPmvilDialogLoading(customPmvilDialog);
        }
    }

    private void showPmvilDialogLoading(PmvilCustomDlg customPmvilDg) {
        if (!customPmvilDg.isShowing() && !isFinishing()) {
            customPmvilDg.show();
        }
    }

    public void hindPmvilLoading() {
        if (customPmvilDialog != null && customPmvilDialog.isShowing() && !isFinishing()) {
            customPmvilDialog.dismiss();
        }
    }

    protected abstract void showPmvilUi();

    protected abstract void clickePmvil();

    protected abstract void loadPmvilData();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        PmvilAtyMd.popPmvilAty(pmvilAy);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent pmvilev) {
        if (pmvilev.getAction() == MotionEvent.ACTION_DOWN) {
            View pmvilv = getCurrentFocus();
            if (isShouldPmvilHideInput(pmvilv, pmvilev)) {
                pmvilv.clearFocus();
                hidePmvilSoftInput();
            }
        }
        return super.dispatchTouchEvent(pmvilev);
    }

    private boolean isShouldPmvilHideInput(View pmvilv, MotionEvent pmvilevent) {
        if (pmvilv != null && (pmvilv instanceof EditText)) {
            int[] pmvilLl = {0, 0};
            pmvilv.getLocationInWindow(pmvilLl);
            int pmvilLeft = pmvilLl[0], pmvilTop = pmvilLl[1], pmvilBottom = pmvilTop + pmvilv.getHeight(), pmvilRight = pmvilLeft + pmvilv.getWidth();
            return !(pmvilevent.getRawX() > pmvilLeft && pmvilevent.getRawX() < pmvilRight && pmvilevent.getY() > pmvilTop && pmvilevent.getY() < pmvilBottom);
        }
        return false;

    }

    private void hidePmvilSoftInput() {
        InputMethodManager pmvilIm = (InputMethodManager) pmvilAy.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (pmvilIm != null) {
            pmvilIm.hideSoftInputFromWindow(pmvilAy.getWindow().getDecorView().getWindowToken(), 0);
        }
    }


    public void outpmvilLongin() {
        hindPmvilLoading();
        PmvilSpMd.putPmvaParam(this, "pmvil_userId_pm", "");
        PmvilSpMd.putPmvaParam(this, "pmvil_uuId_pm", "");
        startActivity(new Intent(pmvilAy, PmvilLoginVy.class));
        PmvilAtyMd.finishPmvilAllAty();
    }

    public void tostPmvilErrMsg(String pmvilMsg) {
        hindPmvilLoading();
        PmvilTosMd.showPmvaMsg(pmvilMsg);
    }

    public void getCheckPmvilPage(boolean isPmvilFinish) {
        showPmvilLoading();
        PmvilHttpRequest.getCheckPmvilPage(this, new PmvilCallBack<PmvilCheckPage>() {
            @Override
            public void onPmvilSucc(String pmvilMethod, PmvilCheckPage pmvilContent) {
                hindPmvilLoading();
                if (pmvilContent.getPmvilauthType() != null) {
                    if (pmvilContent.getPmvilauthType().equals("pmvil_user")) {
                        startActivity(new Intent(pmvilAy, PmvilSourceVy.class));
                        if (isPmvilFinish) finish();
                    } else if (pmvilContent.getPmvilauthType().equals("pmvil_ht") || pmvilContent.getPmvilauthType().equals("pmvil_ocr")) {
                        Intent pmvilIntent = new Intent(pmvilAy, PmvilPlaceVy.class);
                        if (pmvilContent.getPmvilauthType().equals("pmvil_ht")) {
                            pmvilIntent.putExtra("pmvil_isface", true);
                        } else {
                            pmvilIntent.putExtra("pmvil_isface", false);
                        }
                        pmvilIntent.putExtra("pmvil_ischange", pmvilContent.isPmvilmodifyCertificate());
                        startActivity(pmvilIntent);
                        if (isPmvilFinish) finish();
                    } else if (pmvilContent.getPmvilauthType().equals("pmvil_bank")) {
                        startActivity(new Intent(pmvilAy, PmvilBlockVy.class));
                        if (isPmvilFinish) finish();
                    }
                } else {
                    if (pmvilContent.getPmvilpageNum() == -2) {
                        Intent pmvilBlackIntent = new Intent(pmvilAy, PmvilSellerVy.class);
                        pmvilBlackIntent.putExtra("pmvil_black", true);
                        startActivity(pmvilBlackIntent);
                        if (isPmvilFinish) finish();
                    } else if (pmvilContent.getPmvilpageNum() == -1) {
                        startActivity(new Intent(pmvilAy, PmvilCensorVy.class));
                        if (isPmvilFinish) finish();
                    } else if (pmvilContent.getPmvilpageNum() == 1) {
                        Intent pmvilSellerIntent = new Intent(pmvilAy, PmvilListingVy.class);
                        pmvilSellerIntent.putExtra("pmvil_seller", pmvilContent);
                        sendAdjPmvilEvent(PmvilAjustinfse.pmvil_gdrgetj);
                        sendAdjPmvilEvent(PmvilAjustinfse.pmvil_fdgrger);
                        startActivity(pmvilSellerIntent);
                        if (isPmvilFinish) finish();
                    } else if (pmvilContent.getPmvilpageNum() == 4) {
                        Intent pmvilSellerIntent = new Intent(pmvilAy, PmvilSellerVy.class);
                        pmvilSellerIntent.putExtra("pmvil_black", false);
                        pmvilSellerIntent.putExtra("pmvil_seller", pmvilContent);
                        startActivity(pmvilSellerIntent);
                        if (isPmvilFinish) finish();
                    }
                }
            }

            @Override
            public void onPmvilLogOn(String pmvilMod, PmvilCheckPage pmvilCtent) {
                outpmvilLongin();
            }

            @Override
            public void onPmvilOther(String pmvilMhod, PmvilCheckPage pmvilCnt, String pmvilCode, String pmvilMsg) {
                tostPmvilErrMsg(pmvilMsg);
            }

            @Override
            public void onPmvilFailure(String pmvilMod, String pmvilError) {
                tostPmvilErrMsg(pmvilError);
            }
        });
    }

    public void showPmvilPermisDlg(boolean pmvilAgenPer, int pmvilLyId) {
        //R.layout.permis_pmvil_dlg
        //R.layout.permis_agen_pmvil_dlg
        AlertDialog.Builder permisPmvilBlder = new AlertDialog.Builder(this, R.style.pm_dlg_stl);
        View pmvilPermisView = this.getLayoutInflater().inflate(pmvilLyId, null);
        permisPmvilBlder.setView(pmvilPermisView);
        permisPmvilBlder.setCancelable(false);
        AlertDialog pmvilPermisDlg = permisPmvilBlder.create();
        pmvilPermisDlg.show();
        if (pmvilAgenPer) {
            ImageView pmvilPmerIvClost = pmvilPermisView.findViewById(R.id.permis_pmvildlg_close);
            TextView pmvilPmerTvQx = pmvilPermisView.findViewById(R.id.permis_pmvildlg_tv_qx);
            TextView pmvilPmerTvTy = pmvilPermisView.findViewById(R.id.permis_pmvildlg_tv_ty);
            pmvilPmerIvClost.setOnClickListener(v -> pmvilPermisDlg.dismiss());
            pmvilPmerTvQx.setOnClickListener(v -> pmvilPermisDlg.dismiss());
            pmvilPmerTvTy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    pmvilPermisDlg.dismiss();
                    Intent pmvilSettingIntent = new Intent();
                    pmvilSettingIntent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    pmvilSettingIntent.setData(Uri.fromParts("package", BuildConfig.APPLICATION_ID, null));
                    startActivity(pmvilSettingIntent);
                }
            });
        } else {
            ImageView pmvileImClose = pmvilPermisView.findViewById(R.id.pr_pmvil_iv_close);
            TextView pmvilTvIno = pmvilPermisView.findViewById(R.id.pr_pmvil_tv_ino);
            pmvileImClose.setOnClickListener(v -> pmvilPermisDlg.dismiss());
            pmvilTvIno.setOnClickListener(v -> pmvilPermisDlg.dismiss());
        }

    }

    public void goToPmvilXyAty(String pmvilTitle, String pmvilUrl) {
        Intent pmvilWebIntent = new Intent(this, PmvilWebAtVy.class);
        pmvilWebIntent.putExtra("pmvil_title", pmvilTitle);
        pmvilWebIntent.putExtra("pmvil_url", pmvilUrl);
        startActivity(pmvilWebIntent);
    }

    public void showPmvilWaliuDlg() {
        AlertDialog.Builder wlPmvilBlder = new AlertDialog.Builder(this, R.style.pm_dlg_stl);
        View pmvilWlrView = this.getLayoutInflater().inflate(R.layout.wl_rl_pmvil_dlg, null);
        wlPmvilBlder.setView(pmvilWlrView);
        wlPmvilBlder.setCancelable(false);
        AlertDialog pmvilWlDlg = wlPmvilBlder.create();
        pmvilWlDlg.show();
        pmvilShowWlu(pmvilWlrView, pmvilWlDlg);
    }

    private void pmvilShowWlu(View pmvilWlrView, AlertDialog pmvilWlDlg) {
        ImageView wlPmvileImClose = pmvilWlrView.findViewById(R.id.wl_pmvildlg_close);
        TextView wlPmvilTvjx = pmvilWlrView.findViewById(R.id.wl_pmvildlg_tv_jx);
        TextView wlPmvilTvtc = pmvilWlrView.findViewById(R.id.wl_pmvildlg_tv_tc);
        wlPmvileImClose.setOnClickListener(v -> pmvilWlDlg.dismiss());
        wlPmvilTvjx.setOnClickListener(v -> pmvilWlDlg.dismiss());
        wlPmvilTvtc.setOnClickListener(v -> {
            pmvilWlDlg.dismiss();
            finish();
        });
    }

    public  void sendAdjPmvilEvent(String adjPmvilEvent) {
        if (TextUtils.isEmpty(adjPmvilEvent)) return;
        if (!Adjust.isEnabled()) return;
        AdjustEvent adjustEvent = new AdjustEvent(adjPmvilEvent);
        Adjust.trackEvent(adjustEvent);
    }
}
